using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Entity;
using Publicuse.Interface;
using Publicuse.Interface;
using Publicuse.Util;

namespace Publicuse.Export
{
  /// <summary>
  /// 获取节假日信息辅助类
  /// </summary>
  public class HolidayExport
  {
    /// <summary>
    /// 根据年份获取该年所有的节假日信息
    /// </summary>
    /// <param name="year"></param>
    /// <returns></returns>
    public static int GeHolidayType(DateTime selectTime, IMySqlSugarService mySqlDapper)
    {
      int type = -1;
      var tableName = TableAtributeUtil.GetTable<Holiday>();
      var year = selectTime.ToString("yyyy");
      string sql = string.Format(" select * from {0} where hYear={1};", tableName, year);
      HolidayQuery holidayQuery = new HolidayQuery() { sql = sql };
      string msg = string.Empty;
      List<Holiday> holidays = new List<Holiday>();
      holidays = mySqlDapper.Query<Holiday>(sql, out msg);
      if (string.IsNullOrEmpty(msg))
      {
        if (holidays.Count > 0)
        {
          var havdData = holidays
            .Where(o => selectTime >=Convert.ToDateTime(o.hsTime) && selectTime <= Convert.ToDateTime(o.heTime))
            .ToList();
          if (havdData.Count > 0)
          {
            //获取节假日对应字典值
            tableName = TableAtributeUtil.GetTable<Dictionaries>();
            sql = string.Format(
              "select * from {0} where dName like '%{1}%' and dStatus=1",
              tableName,
              havdData[0].hName
            );
            DictionariesQuery query = new DictionariesQuery() { sql = sql };
            List<Dictionaries> list = new List<Dictionaries>();
            list = mySqlDapper.Query<Dictionaries>(sql, out msg);
            if (list.Count > 0)
            {
              type = list[0].dValue.GetIntValue();
            }
            else
            {
              if (havdData[0].hName.Contains("元旦"))
                type = 1;
              else if (havdData[0].hName.Contains("清明"))
                type = 2;
              else if (havdData[0].hName.Contains("五一"))
                type = 3;
              else if (havdData[0].hName.Contains("端午"))
                type = 4;
              else if (havdData[0].hName.Contains("中秋"))
                type = 5;
              else if (havdData[0].hName.Contains("国庆"))
                type = 6;
              else if (havdData[0].hName.Contains("春节"))
                type = 7;
            }
          }
        }
      }
      return type;
    }

    /// <summary>
    /// 节假日导出数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportData(
      string fileName,
      string excelName,
      List<Holiday> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          foreach (Holiday item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //节假日名称
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.hName);

            //年份
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.hYear);

            //开始日期
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.hsTime);

            //结束日期
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.heTime);

            //状态
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.status == 1 ? "正常" : "停用");

            //创建时间
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //修改时间
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uTime?.ToString("yyyy-MM-dd HH:mm:sss"));

            //备注
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.Remark);
          }
        },
        out msg,
        2
      );
      return ms;
    }
  }
}
